//
//  YCTabView.m
//  LOLBox
//
//  Created by mac on 15-3-16.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCTabView.h"

@interface YCTabView ()
{
    UIView *_customBar;
    UIButton *_bubby;
    UIButton *_me;
    UIButton *_qworld;
    UIButton *_recent;
    UIButton *_bbj;
    void (^_aBlock)(NSInteger);
}

@end

@implementation YCTabView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self createCustomTabBar];
        _customBar = [[UIView alloc]init];
        _customBar.backgroundColor = [UIColor redColor];
    }
    return self;
}

//创建自定义tabbar
- (void)createCustomTabBar
{
    _customBar.frame = CGRectMake(0, 0, self.frame.size.width, 50);
    
    UIImage *bubbyNor = [UIImage imageNamed:@"tab_btn_nor0"];
    UIImage *bubbyPress = [UIImage imageNamed:@"tab_btn_sel0"];
    
    UIImage *meNor = [UIImage imageNamed:@"tab_btn_nor6"];
    UIImage *mePress = [UIImage imageNamed:@"tab_btn_sel6"];
    
    UIImage *qworldNor = [UIImage imageNamed:@"tab_btn_nor3"];
    UIImage *qworldPress = [UIImage imageNamed:@"tab_btn_sel3"];
    
    UIImage *recentNor = [UIImage imageNamed:@"tab_btn_nor4"];
    UIImage *recentPress = [UIImage imageNamed:@"tab_btn_sel4"];
    
    UIImage *bbjImg = [UIImage imageNamed:@"tab_btn_nor5"];
    UIImage *bbqImg = [UIImage imageNamed:@"tab_btn_sel5"];
    
    UIImage *tab_nor = [UIImage imageNamed:@"tab_nor"];
    UIImage *tab_sel = [UIImage imageNamed:@"tab_sel"];
    
    _bubby = [UIButton buttonWithType:UIButtonTypeCustom];
    _me = [UIButton buttonWithType:UIButtonTypeCustom];
    _qworld = [UIButton buttonWithType:UIButtonTypeCustom];
    _recent = [UIButton buttonWithType:UIButtonTypeCustom];
    _bbj = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_bubby setBackgroundImage:tab_nor forState:UIControlStateNormal];
    [_bubby setBackgroundImage:tab_sel forState:UIControlStateSelected];
    [_bubby setImage:bubbyNor forState:UIControlStateNormal];
    [_bubby setImage:bubbyPress forState:UIControlStateSelected];
    [_bubby setTitle:@"资讯" forState:UIControlStateNormal];
    
    [_me setBackgroundImage:tab_nor forState:UIControlStateNormal];
    [_me setBackgroundImage:tab_sel forState:UIControlStateSelected];
    [_me setImage:meNor forState:UIControlStateNormal];
    [_me setImage:mePress forState:UIControlStateSelected];
    [_me setTitle:@"视频" forState:UIControlStateNormal];

    
    [_qworld setBackgroundImage:tab_nor forState:UIControlStateNormal];
    [_qworld setBackgroundImage:tab_sel forState:UIControlStateSelected];
    [_qworld setImage:qworldNor forState:UIControlStateNormal];
    [_qworld setImage:qworldPress forState:UIControlStateSelected];
    [_qworld setTitle:@"英雄" forState:UIControlStateNormal];

    
    [_recent setBackgroundImage:tab_nor forState:UIControlStateNormal];
    [_recent setBackgroundImage:tab_sel forState:UIControlStateSelected];
    [_recent setImage:recentNor forState:UIControlStateNormal];
    [_recent setImage:recentPress forState:UIControlStateSelected];
    [_recent setTitle:@"联盟资料" forState:UIControlStateNormal];

    
    [_bbj setBackgroundImage:tab_nor forState:UIControlStateNormal];
    [_bbj setBackgroundImage:tab_sel forState:UIControlStateSelected];
    [_bbj setImage:bbjImg forState:UIControlStateNormal];
    [_bbj setImage:bbqImg forState:UIControlStateSelected];
    [_bbj setTitle:@"更多" forState:UIControlStateNormal];

    _bubby.titleLabel.font = [UIFont systemFontOfSize:13];
    _bubby.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
    _bubby.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
    _bubby.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    _me.titleLabel.font = [UIFont systemFontOfSize:13];
    _me.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
    _me.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
    _me.titleLabel.textAlignment = NSTextAlignmentCenter;

    _qworld.titleLabel.font = [UIFont systemFontOfSize:13];
    _qworld.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
    _qworld.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
    _qworld.titleLabel.textAlignment = NSTextAlignmentCenter;

    _recent.titleLabel.font = [UIFont systemFontOfSize:13];
    _recent.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
    _recent.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
    _recent.titleLabel.textAlignment = NSTextAlignmentCenter;

    _bbj.titleLabel.font = [UIFont systemFontOfSize:13];
    _bbj.imageEdgeInsets = UIEdgeInsetsMake(-18, 15, 0, 1);
    _bbj.titleEdgeInsets = UIEdgeInsetsMake(25, -20, 0, 0);
    _bbj.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    CGFloat width = self.frame.size.width / 5;
    //CGFloat height = width;
    
    _bubby.frame = CGRectMake(0, 0, width, 50);
    _me.frame = CGRectMake(CGRectGetMaxX(_bubby.frame), 0,width, 50);
    _qworld.frame = CGRectMake(CGRectGetMaxX(_me.frame), 0, width,50);
    _recent.frame = CGRectMake(CGRectGetMaxX(_qworld.frame), 0, width, 50);
    _bbj.frame = CGRectMake(CGRectGetMaxX(_recent.frame), 0, width, 50);
    
    [_bubby addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [_me addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [_qworld addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [_recent addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bbj addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置tag，用来和tabbarcontroller的selectedIndex关联
    _bubby.tag = 0;
    _me.tag = 1;
    _qworld.tag = 2;
    _recent.tag = 3;
    _bbj.tag = 4;
    
    _bubby.selected = YES;
//    [self addSubview:bg];

    [self addSubview:_bubby];
    [self addSubview:_me];
    [self addSubview:_qworld];
    [self addSubview:_recent];
    [self addSubview:_bbj];
    //[self addSubview:_customBar];
}

- (void)tapButton:(UIButton *)sender
{
    _bubby.selected = NO;
    _me.selected = NO;
    _qworld.selected = NO;
    _recent.selected = NO;
    _bbj.selected = NO;
    
    sender.selected = YES;
    //跳转到指定tag的viewcontroller ,传值
    _aBlock(sender.tag);
    
    //tbc.selectedIndex = sender.tag;
}

- (void)tranValue:(void (^)(NSInteger))aBlock
{
    _aBlock = aBlock;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
